package com.packet.controller;

import com.packet.pojo.Examine;
import com.packet.service.FinanceQueryService;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @className:FinanceDownloadFile
 * @Author:wallet_packet Date:2023-12-10 9:41
 * @Describe:
 */
@Controller
public class FinanceDownloadFile {
    @Autowired
    FinanceQueryService financeQueryService;

    @RequestMapping("/finance/download-file")
    public void downloadAllExamine(HttpServletResponse response) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();//创建HSSFWorkbook对象,  excel的文档对象
        HSSFSheet sheet = workbook.createSheet("信息表"); //excel的表单
        List<Examine> examines = financeQueryService.selectExamineAll();

        String fileName = "userinf"  + ".xls";//设置要导出的文件的名字



        int rowNum = 1;
        String[] headers = { "账单id", "审批人id", "审批结果", "审批理由"};
        //headers表示excel表中第一行的表头
        HSSFRow row = sheet.createRow(0);
        //在excel表中添加表头
        for(int i=0;i<headers.length;i++){
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }
        //在表中存放查询到的数据放入对应的列
        for (Examine examine : examines) {
            HSSFRow row1 = sheet.createRow(rowNum);
            row1.createCell(0).setCellValue(examine.getBid());
            row1.createCell(1).setCellValue(examine.getSid());
            row1.createCell(2).setCellValue(examine.getResult());
            row1.createCell(3).setCellValue(examine.getDetail());
            rowNum++;
        }

        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());


    }

}
